// pages/searchPage/searchPage.js
const call = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchLogs: [],
    getResult: false,
    resultList: [],
    pageNum: 1,
    isLoading: false,
    bottomTip: '',
    inputValue: ''
  },
  cancel () {
    const getResult = this.data.getResult
    if (getResult){
      this.setData({
        resultList: [],
        inputValue: '',
        getResult: false
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
    
  },

  toSearchBybtn (e) {
    console.log('toSearchBybtn', e)
    this.toSearch(e.detail.value.content)
  },

  toSearchBykey (e) {
    console.log("toSearchBykey", e)
    this.toSearch(e.detail.value)
  },

  searchByrecord(e) {
    console.log("searchByrecord", e)
    const content = e.currentTarget.dataset.content
    this.setData({
      inputValue: content
    })
    this.toSearch(content)
  },

  toSearch (res) {
    let searchLogs = wx.getStorageSync('searchLogs') || []
    const repetition = searchLogs.indexOf(res) //返回重复字段的索引，若无重复返回-1
    if (repetition != -1){
      searchLogs.splice(repetition, 1) //从repetition处开始，删除一个（即重复处本身的记录）
    }
    searchLogs.unshift(res) //在searchLogs头部插入res（unshift方法会改变原数组，返回新的长度）
    searchLogs = searchLogs.splice(0, 25) //删除数组前25个字段，返回被删除的项目（splice会改变原数组）
    wx.setStorageSync('searchLogs', searchLogs)
    this.setData({
      searchLogs: searchLogs,
      getResult: true,
      searchContent: res,
      bottomTip: ''
    })
    wx.showLoading({
      title: '搜索中',
    })
    const openId = wx.getStorageSync('token')
    const that = this
    call.requestGet("jus/searchJobs",{
        openId: openId,
        name:res,
        pageNum:1
    }, function (e) {
      console.log('搜索结果', e)
      if(e.length){
        that.setData({
          resultList: e
        })
      } else {
        that.setData({
          resultList: '',
          bottomTip: '～搜索内容不在这个星球～'
        })
      }
      wx.hideLoading()
    }, function () {
      wx.hideLoading()
    })
  },

  delSearchLogs () {
    wx.removeStorageSync('searchLogs')
    this.setData({
      searchLogs: []
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      searchLogs: (wx.getStorageSync('searchLogs') || [])
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了")
    const that = this
    const openId = wx.getStorageSync('token');
    that.setData({
      isLoading: true
    })
    const pageNum = ++that.data.pageNum
    const searchContent = that.data.searchContent
    call.requestGet('jus/searchJobs',
      {
        openId: openId,
        name: searchContent,
        pageNum: pageNum
      },
      function (res) {
        console.log(res)
        if (res.length) {
          that.setData({
            isLoading: false,
            resultList: that.data.resultList.concat(res)
          })
        } else {
          that.setData({
            isLoading: false,
            bottomTip: '哎哟，都被你看光了～',
            pageNum: pageNum - 1
          })
        }
      },
      function (res) {
        console.log(res)
        that.setData({
          isLoading: false,
          bottomTip: '啊喔，加载失败～'
        })
      }
    )
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})